package com.lyh.hkcloud.util.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import androidx.core.content.ContextCompat;

public class AppUtil {

    //文件存储请求码
    public final static int REQUEST_CODE_PERMISSION_STORAGE = 100;

    //二维码扫码请求码，
    public final static int REQUEST_CODE_PERMISSION_SCAN = 101;


    public final static String[] STORAGE_PERMISSIONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    public final static String[] SCAN_PERMISSIONS = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.VIBRATE,
    };

    /**
     * 获取版本名称
     *
     * @param context 上下文
     * @return 版本名称
     */
    public static String getVersionName(Context context) {

        //获取包管理器
        PackageManager pm = context.getPackageManager();
        //获取包信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            //返回版本号
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return null;

    }

    /**
     * 获取版本号
     *
     * @param context 上下文
     * @return 版本号
     */
    public static int getVersionCode(Context context) {

        //获取包管理器
        PackageManager pm = context.getPackageManager();
        //获取包信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            //返回版本号
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return 0;

    }

    /**
     * 获取文件保存路径 sdcard根目录/download/文件名称
     *
     * @param fileUrl
     * @return
     */
    public static String getSaveFilePath(Activity activity, String fileUrl) {

        if (!checkAndRequestPermissions(activity, STORAGE_PERMISSIONS, REQUEST_CODE_PERMISSION_STORAGE)) {
            ToastUtils.makeText(activity, "获取文件访问权限失败，无法继续。", ToastUtils.LENGTH_SHORT).show();
            return "";
        }
        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);//获取文件名称
        return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/" + fileName;
    }


    @SuppressLint("NewApi")
    public static boolean checkAndRequestPermissions(Activity activity, String[] permissions, int requestCode) {
        if (!lacksPermissions(activity, permissions)) {
            activity.requestPermissions(permissions, requestCode);
            return false;
        }
        return true;
    }

    public static boolean checkPermissionsByCode(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }


    public static boolean lacksPermissions(Activity activity, String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_DENIED) {
                LogUtil.e("TAG", "-------没有开启权限");
                return false;
            }
        }
        LogUtil.e("TAG", "-------权限已开启");
        return true;
    }

}
